import jieba
#主函数
def main():
    txt = getText("sanguo.txt")
    words = cutWords(txt)
    result = countWord(words)
    print_f(result)
#获取文本
def getText(path):
    txt = open(path, 'r', encoding='utf-8').read()
    return txt
#对获取到的文本进行分词
def cutWords(txt):
    words = jieba.lcut(txt)
    return words
#进行统计
def countWord(words):
    counts = dict()
    for word in words:
        if(len(word)==1):
            continue
        else:
            counts[word] = counts.get(word, 0) + 1
    result = list(counts.items())
    result.sort(key=lambda x:x[1], reverse=True)
    return result
#输出
def print_f(result, n=10):
    for i in range(n):
        word, count = result[i]
        print(word, count)
main()
